package com.excesys.exsecs.access.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.excesys.exsecs.access.dto.AccessBuildingsDTO;
import com.excesys.exsecs.access.entity.AccessBuildings;
import com.excesys.exsecs.access.vo.AccessBuildingsExcelVO;
import com.excesys.exsecs.common.core.util.R;
import com.excesys.exsecs.common.excel.annotation.RequestExcel;
import com.excesys.exsecs.common.log.annotation.SysLog;
import com.excesys.exsecs.access.service.AccessBuildingsService;
import org.springframework.security.access.prepost.PreAuthorize;
import com.excesys.exsecs.common.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.List;
import java.util.Objects;

/**
 * 区域管理-建筑管理
 *
 * @author lxc
 * @date 2024-09-13 11:48:31
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/accessBuildings" )
@Tag(description = "accessBuildings" , name = "区域管理-建筑管理管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class AccessBuildingsController {

    private final  AccessBuildingsService accessBuildingsService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param buildingsDTO 区域管理-建筑管理
     * @return
     */
    @Operation(summary = "分页查询", description = "分页查询")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('access_accessBuildings_view')")
    public R getBuildingsPage(Page page, AccessBuildingsDTO buildingsDTO) {
        return R.ok(accessBuildingsService.queryPage(page, buildingsDTO));
    }

    @Operation(summary = "根据条件查询不分页", description = "条件查询")
    @PostMapping("/getBuildingsBy")
    @PreAuthorize("@pms.hasPermission('access_accessBuildings_view')")
    public R getBuildingsBy(@RequestBody AccessBuildingsDTO buildingsDTO) {
        return R.ok(accessBuildingsService.list(accessBuildingsService.buildQueryWrapper(buildingsDTO)));
    }

    /**
     * 通过id查询区域管理-建筑管理
     * @param buildingsId id
     * @return R
     */
    @Operation(summary = "通过id查询", description = "通过id查询")
    @GetMapping("/{buildingsId}")
    @PreAuthorize("@pms.hasPermission('access_accessBuildings_view')")
    public R getById(@PathVariable("buildingsId") String buildingsId) {
        return R.ok(accessBuildingsService.getById(buildingsId));
    }

    /**
     * 新增区域管理-建筑管理
     * @param buildings 区域管理-建筑管理
     * @return R
     */
    @Operation(summary = "新增区域管理-建筑管理", description = "新增区域管理-建筑管理")
    @SysLog("新增区域管理-建筑管理")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('access_accessBuildings_add')")
    public R save(@RequestBody AccessBuildings buildings) {
        return R.ok(accessBuildingsService.save(buildings));
    }

    /**
     * 修改区域管理-建筑管理
     * @param buildings 区域管理-建筑管理
     * @return R
     */
    @Operation(summary = "修改区域管理-建筑管理", description = "修改区域管理-建筑管理")
    @SysLog("修改区域管理-建筑管理")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('access_accessBuildings_edit')")
    public R updateById(@RequestBody AccessBuildings buildings) {
        return R.ok(accessBuildingsService.updateById(buildings));
    }

    /**
     * 通过id删除区域管理-建筑管理
     * @param buildingsId id
     * @return R
     */
    @Operation(summary = "通过id删除区域管理-建筑管理", description = "通过id删除区域管理-建筑管理")
    @SysLog("通过id删除区域管理-建筑管理")
    @DeleteMapping("/{buildingsId}")
    @PreAuthorize("@pms.hasPermission('access_accessBuildings_del')")
    public R removeById(@PathVariable String buildingsId) {
        return R.ok(accessBuildingsService.removeById(buildingsId));
    }

    @Operation(summary = "通过ids删除区域管理-建筑管理", description = "通过ids删除区域管理-建筑管理")
    @SysLog("通过ids删除区域管理-建筑管理")
    @DeleteMapping("/deleteIds")
    @PreAuthorize("@pms.hasPermission('access_accessBuildings_del')")
    public R removeByIds(@RequestBody List<String> ids) {
        return R.ok(accessBuildingsService.removeByIds(ids));
    }

    /**
     * 导出excel 表格
     * @return
     */

    @Operation(summary = "导出所有数据", description = "导出所有数据")
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('access_accessBuildings_export')")
    public List<AccessBuildingsExcelVO> export(AccessBuildingsDTO buildings) {
        return accessBuildingsService.listBuildings(buildings);
    }

    /**
     * 导入建筑
     * @param excelVOList 建筑管理列表
     * @param bindingResult 错误信息列表
     * @return ok fail
     */
    @PostMapping("/import")
    @PreAuthorize("@pms.hasPermission('access_accessBuildings_import')")
    public R importBuilding(@RequestExcel List<AccessBuildingsExcelVO> excelVOList, BindingResult bindingResult) {
        return accessBuildingsService.importBuildings(excelVOList, bindingResult);
    }

    @Operation(summary = "自动生成楼栋编码", description = "自动生成楼栋编码")
    @GetMapping("/getBuildingCode")
    @PreAuthorize("@pms.hasPermission('access_accessBuildings_view')")
    public R getBuildingCode(String code, int level, String suffix) throws ParseException {
        return R.ok(accessBuildingsService.GetBuildingCodeBy(code, level, suffix));
    }
}